import { ref } from "vue";
import { defineStore } from "pinia";
import { addRoute } from "@/router";

export const useUserStore = defineStore(
  "user",
  () => {
    const user = ref({
      roles: "",
      token: "",
    });

    const setUser = (roles: string, token: string) => {
      user.value.roles = roles;
      user.value.token = token;
    };

    const getUser = () => {
      if (user.value.roles === "") return null;
      if (user.value.token === "") return null;

      return user.value;
    };

    const setRoutes = () => {
      addRoute(user.value.roles);
    };

    return { user, setUser, getUser, setRoutes };
  },
  {
    persist: true,
  }
);
